package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Entity.LearnHistory;
import org.example.Entity.Word;
import org.example.Entity.WordBook;
import org.example.Mapper.LearnHistoryMapper;
import org.example.Mapper.WordBookMapper;
import org.example.Mapper.WordMapper;
import org.example.Service.UserService;
import org.example.Service.WordBookService;
import org.example.Utils.HttpUtil;
import org.example.Vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
public class WordBookServiceImpl extends ServiceImpl<WordBookMapper, WordBook> implements WordBookService {

    @Autowired
    private WordBookMapper wordBookMapper;

    @Resource
    private HttpUtil httpUtil;

    @Override
    public R know(WordBook word,String username) {
        Integer userId = httpUtil.getUserId(username);
        LambdaQueryWrapper<WordBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordBook::getWord,word.getWord());
        WordBook wordBook = wordBookMapper.selectOne(wrapper);
        if (wordBook == null){
            // 不存在 插入状态为认识的单词
            WordBook wb = WordBook.builder().word(word.getWord()).definition(word.getDefinition()).userId(userId).blur(0).build();
            wordBookMapper.insert(wb);
            return R.ok("插入成功");
        }
        // 已经存在 修改状态为认识
        wordBook.setBlur(0);
        this.updateById(wordBook);
        return R.ok("更新成功");
    }

    @Override
    public R blur(WordBook word,String username) {
        Integer userId = httpUtil.getUserId(username);
        LambdaQueryWrapper<WordBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordBook::getWord,word.getWord());
        WordBook wordBook = wordBookMapper.selectOne(wrapper);
        if (wordBook == null){
            // 不存在 插入状态为模糊的单词
            WordBook wb = WordBook.builder().word(word.getWord()).definition(word.getDefinition()).userId(userId).blur(1).build();
            wordBookMapper.insert(wb);
            return R.ok("插入成功");
        }
        // 已经存在 修改状态为模糊
        wordBook.setBlur(1);
        this.updateById(wordBook);
        return R.ok("更新成功");
    }

    @Override
    public R getAllKnowWord(String username) {
        Integer userId = httpUtil.getUserId(username);
        LambdaQueryWrapper<WordBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordBook::getBlur,0);
        wrapper.eq(WordBook::getUserId,userId);
        List<WordBook> list = this.list(wrapper);
        return R.ok(list);
    }

    @Override
    public R getAllBlurWord(String username) {
        Integer userId = httpUtil.getUserId(username);
        LambdaQueryWrapper<WordBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordBook::getBlur,1);
        wrapper.eq(WordBook::getUserId,userId);
        List<WordBook> list = this.list(wrapper);
        return R.ok(list);
    }
}
